%=== 数据加载 ===
relocation_data = readtable('relocation_data.xlsx','VariableNamingRule','preserve');
disp(plot_info.Properties.VariableNames);
plot_info = readtable('plot_info.csv','VariableNamingRule','preserve');

% 提取必要变量
plot_ids = plot_info.("地块ID");
courtyard_ids = plot_info.("院落ID");
plot_area = plot_info.("地块面积");
is_occupied = plot_info.("是否有住户");

resident_ids = relocation_data.("居民地块ID");  % 假设存在此列
target_candidates = relocation_data.("目标地块ID"); % 假设为搬迁目标对应的地块ID列
resident_area = relocation_data.("居民地块面积");
target_area = relocation_data.("目标地块面积");
resident_courtyards = relocation_data.("居民院落ID");
target_courtyards = relocation_data.("目标院落ID");

n_courtyards = numel(unique(courtyard_ids));
n_residents = length(resident_ids);

%=== 构建院落邻接矩阵（示范用随机邻接，实际请用空间关系构造）===
adjacency_matrix = zeros(n_courtyards);
% 示例：实际用空间或地理数据构造邻接矩阵
% adjacency_matrix(i,j) = 1 表示院落i和j邻接
% 请根据院落坐标或其他信息构造邻接矩阵

%=== 初始化占用状态 ===
occupied_plots = is_occupied; % 0或1

%=== 初始化存储结果 ===
target_plots_selected = nan(n_residents,1);

%=== 定义效益权重 ===
weight_full_courtyard = 10;

%=== 遍历每个居民，选择最优目标地块 ===
for i = 1:n_residents
    current_plot = resident_ids(i);
    current_area = resident_area(i);
    current_courtyard = resident_courtyards(i);
    
    % 获取当前居民所有可选目标地块ID（可选目标地块列可能多个，这里示范一个）
    possible_targets = relocation_data.("候选目标地块ID列"); % 如果有多列，需调整
    
    best_target = NaN;
    max_benefit = -Inf;
    
    for t = 1:length(possible_targets)
        target_plot = possible_targets(t);
        if isnan(target_plot)
            continue;
        end
        
        % 验证目标地块是否有效、空闲
        idx_target = find(plot_ids == target_plot);
        if isempty(idx_target)
            continue;
        end
        if occupied_plots(idx_target) == 1
            continue;
        end
        
        % 面积约束
        A_target = plot_area(idx_target);
        if A_target < current_area || A_target > 1.3 * current_area
            continue;
        end
        
        % 计算邻接效益
        target_courtyard_id = courtyard_ids(idx_target);
        neighbors = find(adjacency_matrix(target_courtyard_id,:) == 1);
        benefit_adjacent = 0;
        for nb = neighbors
            % 判断邻居院落是否腾空
            idxs_in_nb_courtyard = find(courtyard_ids == nb);
            if all(occupied_plots(idxs_in_nb_courtyard) == 0)
                benefit_adjacent = benefit_adjacent + 1;
            end
        end
        
        % 判断目标院落是否腾空（含目标地块腾空后）
        idxs_in_target_courtyard = find(courtyard_ids == target_courtyard_id);
        temp_occupied = occupied_plots;
        temp_occupied(idx_target) = 1; % 标记目标地块搬入后占用
        % 注意：原地块搬出需及时更新，稍后再处理
        
        % 计算腾空完整院落效益
        if all(temp_occupied(idxs_in_target_courtyard) == 0)
            benefit_full_courtyard = 1;
        else
            benefit_full_courtyard = 0;
        end
        
        % 总效益
        total_benefit = benefit_adjacent + weight_full_courtyard * benefit_full_courtyard + A_target;
        
        % 选择最大效益
        if total_benefit > max_benefit
            max_benefit = total_benefit;
            best_target = target_plot;
        end
    end
    
    % 更新搬迁结果
    if ~isnan(best_target)
        target_plots_selected(i) = best_target;
        
        % 更新占用状态：目标地块设为已占用，原地块腾空
        idx_old = find(plot_ids == current_plot);
        idx_new = find(plot_ids == best_target);
        occupied_plots(idx_old) = 0;
        occupied_plots(idx_new) = 1;
    end
end

%=== 统计腾空完整院落数量 ===
unique_courtyards = unique(courtyard_ids);
total_full_courtyards = 0;
for c = unique_courtyards'
    idxs = find(courtyard_ids == c);
    if all(occupied_plots(idxs) == 0)
        total_full_courtyards = total_full_courtyards + 1;
    end
end

%=== 输出结果 ===
fprintf('最终腾空完整院落数量: %d\n', total_full_courtyards);
for i = 1:n_residents
    fprintf('居民地块 %d 搬迁到目标地块 %d\n', resident_ids(i), target_plots_selected(i));
end
